<!doctype html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  
    
      
    

    
  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.0" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="PHP," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.0" />






<meta name="description" content="本文内容来自PHP手册

ifif 结构是很多语言包括 PHP 在内最重要的特性之一，它允许按照条件执行代码片段。PHP 的 if 结构和 C 语言相似：
&amp;lt;?phpif (expr)  statement?&amp;gt;如同在表达式一章中定义的，expr 按照布尔求值。如果 expr 的值为 TRUE，PHP 将执行 statement，如果值为 FALSE ——将忽略 statement。">
<meta property="og:type" content="article">
<meta property="og:title" content="PHP流程控制">
<meta property="og:url" content="http://yoursite.com/2017/05/17/PHP流程控制/index.html">
<meta property="og:site_name" content="EasyBlog">
<meta property="og:description" content="本文内容来自PHP手册

ifif 结构是很多语言包括 PHP 在内最重要的特性之一，它允许按照条件执行代码片段。PHP 的 if 结构和 C 语言相似：
&amp;lt;?phpif (expr)  statement?&amp;gt;如同在表达式一章中定义的，expr 按照布尔求值。如果 expr 的值为 TRUE，PHP 将执行 statement，如果值为 FALSE ——将忽略 statement。">
<meta property="og:updated_time" content="2017-05-17T06:37:24.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="PHP流程控制">
<meta name="twitter:description" content="本文内容来自PHP手册

ifif 结构是很多语言包括 PHP 在内最重要的特性之一，它允许按照条件执行代码片段。PHP 的 if 结构和 C 语言相似：
&amp;lt;?phpif (expr)  statement?&amp;gt;如同在表达式一章中定义的，expr 按照布尔求值。如果 expr 的值为 TRUE，PHP 将执行 statement，如果值为 FALSE ——将忽略 statement。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/05/17/PHP流程控制/"/>





  <title> PHP流程控制 | EasyBlog </title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?07a6d49ae67fe991e0ce3c2f3955e0f9";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">EasyBlog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/05/17/PHP流程控制/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Gasbylei">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="http://gasbylei.github.io/uploads/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="EasyBlog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
            
            
              
                PHP流程控制
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-17T14:36:19+08:00">
                2017-05-17
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/05/17/PHP流程控制/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count" data-disqus-identifier="2017/05/17/PHP流程控制/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>本文内容来自<a href="http://php.net/manual/zh/language.control-structures.php" target="_blank" rel="external">PHP手册</a></p>
</blockquote>
<h3 id="if"><a href="#if" class="headerlink" title="if"></a>if</h3><p>if 结构是很多语言包括 PHP 在内最重要的特性之一，它允许按照条件执行代码片段。PHP 的 if 结构和 C 语言相似：</p>
<p>&lt;?php<br>if (expr)<br>  statement<br>?&gt;<br>如同在表达式一章中定义的，expr 按照布尔求值。如果 expr 的值为 TRUE，PHP 将执行 statement，如果值为 FALSE ——将忽略 statement。</p>
<p>如果 $a 大于 $b，则以下例子将显示 a is bigger than b：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="keyword">if</span> ($a &gt; $b)</div><div class="line">  <span class="keyword">echo</span> <span class="string">"a is bigger than b"</span>;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<h3 id="elseif-else-if"><a href="#elseif-else-if" class="headerlink" title="elseif/else if"></a>elseif/else if</h3><p>elseif，和此名称暗示的一样，是 if 和 else 的组合。和 else 一样，它延伸了 if 语句，可以在原来的 if 表达式值为 FALSE 时执行不同语句。但是和 else 不一样的是，它仅在 elseif 的条件表达式值为 TRUE 时执行语句。例如以下代码将根据条件分别显示 a is bigger than b，a equal to b 或者 a is smaller than b：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="keyword">if</span> ($a &gt; $b) &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"a is bigger than b"</span>;</div><div class="line">&#125; <span class="keyword">elseif</span> ($a == $b) &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"a is equal to b"</span>;</div><div class="line">&#125; <span class="keyword">else</span> &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"a is smaller than b"</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>在同一个 if 语句中可以有多个 elseif 部分，其中第一个表达式值为 TRUE（如果有的话）的 elseif 部分将会执行。在 PHP 中，也可以写成“else if”（两个单词），它和“elseif”（一个单词）的行为完全一样。句法分析的含义有少许区别（如果你熟悉 C 语言的话，与之行为相同），但是底线是两者会产生完全一样的行为。</p>
<p>elseif 的语句仅在之前的 if 和所有之前 elseif 的表达式值为 FALSE，并且当前的 elseif 表达式值为 TRUE 时执行。<br><a id="more"></a></p>
<blockquote>
<p>Note: 必须要注意的是 elseif 与 else if 只有在类似上例中使用花括号的情况下才认为是完全相同。如果用冒号来定义 if/elseif 条件，那就不能用两个单词的 else if，否则 PHP 会产生解析错误。</p>
</blockquote>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"></div><div class="line"><span class="comment">/* 不正确的使用方法： */</span></div><div class="line"><span class="keyword">if</span>($a &gt; $b):</div><div class="line">    <span class="keyword">echo</span> $a.<span class="string">" is greater than "</span>.$b;</div><div class="line"><span class="keyword">else</span> <span class="keyword">if</span>($a == $b): <span class="comment">// 将无法编译</span></div><div class="line">    <span class="keyword">echo</span> <span class="string">"The above line causes a parse error."</span>;</div><div class="line"><span class="keyword">endif</span>;</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">/* 正确的使用方法： */</span></div><div class="line"><span class="keyword">if</span>($a &gt; $b):</div><div class="line">    <span class="keyword">echo</span> $a.<span class="string">" is greater than "</span>.$b;</div><div class="line"><span class="keyword">elseif</span>($a == $b): <span class="comment">// 注意使用了一个单词的 elseif</span></div><div class="line">    <span class="keyword">echo</span> $a.<span class="string">" equals "</span>.$b;</div><div class="line"><span class="keyword">else</span>:</div><div class="line">    <span class="keyword">echo</span> $a.<span class="string">" is neither greater than or equal to "</span>.$b;</div><div class="line"><span class="keyword">endif</span>;</div><div class="line"></div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<h3 id="流程控制的替代语法"><a href="#流程控制的替代语法" class="headerlink" title="流程控制的替代语法"></a>流程控制的替代语法</h3><p>PHP 提供了一些流程控制的替代语法，包括 if，while，for，foreach 和 switch。替代语法的基本形式是把左花括号（{）换成冒号（:），把右花括号（}）分别换成 endif;，endwhile;，endfor;，endforeach; 以及 endswitch;。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span> <span class="keyword">if</span> ($a == <span class="number">5</span>): <span class="meta">?&gt;</span></div><div class="line">A is equal to <span class="number">5</span></div><div class="line"><span class="meta">&lt;?php</span> <span class="keyword">endif</span>; <span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="keyword">if</span> ($a == <span class="number">5</span>):</div><div class="line">    <span class="keyword">echo</span> <span class="string">"a equals 5"</span>;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"..."</span>;</div><div class="line"><span class="keyword">elseif</span> ($a == <span class="number">6</span>):</div><div class="line">    <span class="keyword">echo</span> <span class="string">"a equals 6"</span>;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"!!!"</span>;</div><div class="line"><span class="keyword">else</span>:</div><div class="line">    <span class="keyword">echo</span> <span class="string">"a is neither 5 nor 6"</span>;</div><div class="line"><span class="keyword">endif</span>;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<blockquote>
<p>Note:<br>不支持在同一个控制块内混合使用两种语法</p>
</blockquote>
<h3 id="while"><a href="#while" class="headerlink" title="while"></a>while</h3><p>while 循环是 PHP 中最简单的循环类型。它和 C 语言中的 while 表现地一样。while 语句的基本格式是：</p>
<p>while (expr)<br>    statement<br>while 语句的含意很简单，它告诉 PHP 只要 while 表达式的值为 TRUE 就重复执行嵌套中的循环语句。表达式的值在每次开始循环时检查，所以即使这个值在循环语句中改变了，语句也不会停止执行，直到本次循环结束。有时候如果 while 表达式的值一开始就是 FALSE，则循环语句一次都不会执行。</p>
<p>和 if 语句一样，可以在 while 循环中用花括号括起一个语句组，或者用替代语法：</p>
<p>while (expr):<br>    statement<br>    …<br>endwhile;</p>
<h3 id="do-while"><a href="#do-while" class="headerlink" title="do-while"></a>do-while</h3><p>do-while 循环和 while 循环非常相似，区别在于表达式的值是在每次循环结束时检查而不是开始时。和一般的 while 循环主要的区别是 do-while 的循环语句保证会执行一次（表达式的真值在每次循环结束后检查），然而在一般的 while 循环中就不一定了（表达式真值在循环开始时检查，如果一开始就为 FALSE 则整个循环立即终止）。</p>
<p>do-while 循环只有一种语法：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$i = <span class="number">0</span>;</div><div class="line"><span class="keyword">do</span> &#123;</div><div class="line">   <span class="keyword">echo</span> $i;</div><div class="line">&#125; <span class="keyword">while</span> ($i &gt; <span class="number">0</span>);</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>以上循环将正好运行一次，因为经过第一次循环后，当检查表达式的真值时，其值为 FALSE（$i 不大于 0）而导致循环终止。</p>
<h3 id="for"><a href="#for" class="headerlink" title="for"></a>for</h3><p>for 循环是 PHP 中最复杂的循环结构。它的行为和 C 语言的相似。 for 循环的语法是：</p>
<p>for (expr1; expr2; expr3)<br>    statement<br>第一个表达式（expr1）在循环开始前无条件求值（并执行）一次。</p>
<p>expr2 在每次循环开始前求值。如果值为 TRUE，则继续循环，执行嵌套的循环语句。如果值为 FALSE，则终止循环。</p>
<p>expr3 在每次循环之后被求值（并执行）。</p>
<p>每个表达式都可以为空或包括逗号分隔的多个表达式。表达式 expr2 中，所有用逗号分隔的表达式都会计算，但只取最后一个结果。expr2 为空意味着将无限循环下去（和 C 一样，PHP 暗中认为其值为 TRUE）。这可能不像想象中那样没有用，因为经常会希望用有条件的 break 语句来结束循环而不是用 for 的表达式真值判断。</p>
<p>考虑以下的例子，它们都显示数字 1 到 10：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="comment">/* example 1 */</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> ($i = <span class="number">1</span>; $i &lt;= <span class="number">10</span>; $i++) &#123;</div><div class="line">    <span class="keyword">echo</span> $i;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">/* example 2 */</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> ($i = <span class="number">1</span>; ; $i++) &#123;</div><div class="line">    <span class="keyword">if</span> ($i &gt; <span class="number">10</span>) &#123;</div><div class="line">        <span class="keyword">break</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">echo</span> $i;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">/* example 3 */</span></div><div class="line"></div><div class="line">$i = <span class="number">1</span>;</div><div class="line"><span class="keyword">for</span> (;;) &#123;</div><div class="line">    <span class="keyword">if</span> ($i &gt; <span class="number">10</span>) &#123;</div><div class="line">        <span class="keyword">break</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">echo</span> $i;</div><div class="line">    $i++;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">/* example 4 */</span></div><div class="line"></div><div class="line"><span class="keyword">for</span> ($i = <span class="number">1</span>, $j = <span class="number">0</span>; $i &lt;= <span class="number">10</span>; $j += $i, <span class="keyword">print</span> $i, $i++);</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<h3 id="foreach"><a href="#foreach" class="headerlink" title="foreach"></a>foreach</h3><p>foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象，如果尝试应用于其他数据类型的变量，或者未初始化的变量将发出错误信息。有两种语法：</p>
<p>foreach (array_expression as $value)<br>    statement</p>
<p>foreach (array_expression as $key =&gt; $value)<br>    statement</p>
<p>第一种格式遍历给定的 array_expression 数组。每次循环中，当前单元的值被赋给 $value 并且数组内部的指针向前移一步（因此下一次循环中将会得到下一个单元）。</p>
<p>第二种格式做同样的事，只除了当前单元的键名也会在每次循环中被赋给变量 $key。</p>
<blockquote>
<p>Note:<br>当 foreach 开始执行时，数组内部的指针会自动指向第一个单元。这意味着不需要在 foreach 循环之前调用 reset()。<br>由于 foreach 依赖内部数组指针，在循环中修改其值将可能导致意外的行为。</p>
</blockquote>
<p>可以很容易地通过在 $value 之前加上 &amp; 来修改数组的元素。此方法将以引用赋值而不是拷贝一个值。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$arr = <span class="keyword">array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>);</div><div class="line"><span class="keyword">foreach</span> ($arr <span class="keyword">as</span> &amp;$value) &#123;</div><div class="line">    $value = $value * <span class="number">2</span>;</div><div class="line">&#125;</div><div class="line"><span class="comment">// $arr is now array(2, 4, 6, 8)</span></div><div class="line"><span class="keyword">unset</span>($value); <span class="comment">// 最后取消掉引用</span></div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>$value 的引用仅在被遍历的数组可以被引用时才可用（例如是个变量）。以下代码则无法运行：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="keyword">foreach</span> (<span class="keyword">array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>) <span class="keyword">as</span> &amp;$value) &#123;</div><div class="line">    $value = $value * <span class="number">2</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<blockquote>
<p>Warning<br>数组最后一个元素的 $value 引用在 foreach 循环之后仍会保留。建议使用 unset() 来将其销毁。</p>
</blockquote>
<h4 id="用-list-给嵌套的数组解包"><a href="#用-list-给嵌套的数组解包" class="headerlink" title="用 list() 给嵌套的数组解包"></a>用 list() 给嵌套的数组解包</h4><p>PHP 5.5 增添了遍历一个数组的数组的功能并且把嵌套的数组解包到循环变量中，只需将 list() 作为值提供。list — 把数组中的值赋给一组变量</p>
<p>例如：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$array = [</div><div class="line">    [<span class="number">1</span>, <span class="number">2</span>],</div><div class="line">    [<span class="number">3</span>, <span class="number">4</span>],</div><div class="line">];</div><div class="line"></div><div class="line"><span class="keyword">foreach</span> ($array <span class="keyword">as</span> <span class="keyword">list</span>($a, $b)) &#123;</div><div class="line">    <span class="comment">// $a contains the first element of the nested array,</span></div><div class="line">    <span class="comment">// and $b contains the second element.</span></div><div class="line">    <span class="keyword">echo</span> <span class="string">"A: $a; B: $b\n"</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>以上例程会输出：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">A: <span class="number">1</span>; B: <span class="number">2</span></div><div class="line">A: <span class="number">3</span>; B: <span class="number">4</span></div></pre></td></tr></table></figure>
<p>list() 中的单元可以少于嵌套数组的，此时多出来的数组单元将被忽略：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$array = [</div><div class="line">    [<span class="number">1</span>, <span class="number">2</span>],</div><div class="line">    [<span class="number">3</span>, <span class="number">4</span>],</div><div class="line">];</div><div class="line"></div><div class="line"><span class="keyword">foreach</span> ($array <span class="keyword">as</span> <span class="keyword">list</span>($a)) &#123;</div><div class="line">    <span class="comment">// Note that there is no $b here.</span></div><div class="line">    <span class="keyword">echo</span> <span class="string">"$a\n"</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div><div class="line"></div><div class="line">以上会输出：</div><div class="line"><span class="number">1</span></div><div class="line"><span class="number">3</span></div></pre></td></tr></table></figure>
<p>如果 list() 中列出的单元多于嵌套数组则会发出一条消息级别的错误信息：<code>Notice: Undefined offset:</code></p>
<h3 id="break"><a href="#break" class="headerlink" title="break"></a>break</h3><p>break 结束当前 for，foreach，while，do-while 或者 switch 结构的执行。</p>
<p>break 可以接受一个可选的数字参数来决定跳出几重循环。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line">$arr = <span class="keyword">array</span>(<span class="string">'one'</span>, <span class="string">'two'</span>, <span class="string">'three'</span>, <span class="string">'four'</span>, <span class="string">'stop'</span>, <span class="string">'five'</span>);</div><div class="line"><span class="keyword">while</span> (<span class="keyword">list</span> (, $val) = each($arr)) &#123;</div><div class="line">    <span class="keyword">if</span> ($val == <span class="string">'stop'</span>) &#123;</div><div class="line">        <span class="keyword">break</span>;    <span class="comment">/* You could also write 'break 1;' here. */</span></div><div class="line">    &#125;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"$val&lt;br /&gt;\n"</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">/* 使用可选参数 */</span></div><div class="line"></div><div class="line">$i = <span class="number">0</span>;</div><div class="line"><span class="keyword">while</span> (++$i) &#123;</div><div class="line">    <span class="keyword">switch</span> ($i) &#123;</div><div class="line">    <span class="keyword">case</span> <span class="number">5</span>:</div><div class="line">        <span class="keyword">echo</span> <span class="string">"At 5&lt;br /&gt;\n"</span>;</div><div class="line">        <span class="keyword">break</span> <span class="number">1</span>;  <span class="comment">/* 只退出 switch. */</span></div><div class="line">    <span class="keyword">case</span> <span class="number">10</span>:</div><div class="line">        <span class="keyword">echo</span> <span class="string">"At 10; quitting&lt;br /&gt;\n"</span>;</div><div class="line">        <span class="keyword">break</span> <span class="number">2</span>;  <span class="comment">/* 退出 switch 和 while 循环 */</span></div><div class="line">    <span class="keyword">default</span>:</div><div class="line">        <span class="keyword">break</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<h3 id="continue"><a href="#continue" class="headerlink" title="continue"></a>continue</h3><p>continue 在循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。</p>
<blockquote>
<p>Note: 注意在 PHP 中 switch 语句被认为是可以使用 continue 的一种循环结构。</p>
</blockquote>
<p>continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。默认值是 1，即跳到当前循环末尾。</p>
<h3 id="switch"><a href="#switch" class="headerlink" title="switch"></a>switch</h3><p>switch 语句类似于具有同一个表达式的一系列 if 语句。很多场合下需要把同一个变量（或表达式）与很多不同的值比较，并根据它等于哪个值来执行不同的代码。这正是 switch 语句的用途。</p>
<blockquote>
<p>Note: 注意和其它语言不同，continue 语句作用到 switch 上的作用类似于 break。如果在循环中有一个 switch 并希望 continue 到外层循环中的下一轮循环，用 continue 2。</p>
</blockquote>
<p>下面两个例子使用两种不同方法实现同样的事，一个用一系列的 if 和 elseif 语句，另一个用 switch 语句：</p>
<p><strong>Example #1 switch 结构</strong></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="keyword">if</span> ($i == <span class="number">0</span>) &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"i equals 0"</span>;</div><div class="line">&#125; <span class="keyword">elseif</span> ($i == <span class="number">1</span>) &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"i equals 1"</span>;</div><div class="line">&#125; <span class="keyword">elseif</span> ($i == <span class="number">2</span>) &#123;</div><div class="line">    <span class="keyword">echo</span> <span class="string">"i equals 2"</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">switch</span> ($i) &#123;</div><div class="line">    <span class="keyword">case</span> <span class="number">0</span>:</div><div class="line">        <span class="keyword">echo</span> <span class="string">"i equals 0"</span>;</div><div class="line">        <span class="keyword">break</span>;</div><div class="line">    <span class="keyword">case</span> <span class="number">1</span>:</div><div class="line">        <span class="keyword">echo</span> <span class="string">"i equals 1"</span>;</div><div class="line">        <span class="keyword">break</span>;</div><div class="line">    <span class="keyword">case</span> <span class="number">2</span>:</div><div class="line">        <span class="keyword">echo</span> <span class="string">"i equals 2"</span>;</div><div class="line">        <span class="keyword">break</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p><strong>Example #2 switch 结构可以用字符串</strong></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="keyword">switch</span> ($i) &#123;</div><div class="line"><span class="keyword">case</span> <span class="string">"apple"</span>:</div><div class="line">    <span class="keyword">echo</span> <span class="string">"i is apple"</span>;</div><div class="line">    <span class="keyword">break</span>;</div><div class="line"><span class="keyword">case</span> <span class="string">"bar"</span>:</div><div class="line">    <span class="keyword">echo</span> <span class="string">"i is bar"</span>;</div><div class="line">    <span class="keyword">break</span>;</div><div class="line"><span class="keyword">case</span> <span class="string">"cake"</span>:</div><div class="line">    <span class="keyword">echo</span> <span class="string">"i is cake"</span>;</div><div class="line">    <span class="keyword">break</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>为避免错误，理解 switch 是怎样执行的非常重要。switch 语句一行接一行地执行（实际上是语句接语句）。开始时没有代码被执行。仅当一个 case 语句中的值和 switch 表达式的值匹配时 PHP 才开始执行语句，直到 switch 的程序段结束或者遇到第一个 break 语句为止。如果不在 case 的语句段最后写上 break 的话，PHP 将继续执行下一个 case 中的语句段</p>
<p>在 switch 语句中条件只求值一次并用来和每个 case 语句比较。在 elseif 语句中条件会再次求值。如果条件比一个简单的比较要复杂得多或者在一个很多次的循环中，那么用 switch 语句可能会快一些。</p>
<p>一个 case 的特例是 default。它匹配了任何和其它 case 都不匹配的情况。例如：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="keyword">switch</span> ($i) &#123;</div><div class="line">    <span class="keyword">case</span> <span class="number">0</span>:</div><div class="line">        <span class="keyword">echo</span> <span class="string">"i equals 0"</span>;</div><div class="line">        <span class="keyword">break</span>;</div><div class="line">    <span class="keyword">case</span> <span class="number">1</span>:</div><div class="line">        <span class="keyword">echo</span> <span class="string">"i equals 1"</span>;</div><div class="line">        <span class="keyword">break</span>;</div><div class="line">    <span class="keyword">case</span> <span class="number">2</span>:</div><div class="line">        <span class="keyword">echo</span> <span class="string">"i equals 2"</span>;</div><div class="line">        <span class="keyword">break</span>;</div><div class="line">    <span class="keyword">default</span>:</div><div class="line">        <span class="keyword">echo</span> <span class="string">"i is not equal to 0, 1 or 2"</span>;</div><div class="line">&#125;</div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>case 表达式可以是任何求值为简单类型的表达式，即整型或浮点数以及字符串。不能用数组或对象，除非它们被解除引用成为简单类型。</p>
<h3 id="return"><a href="#return" class="headerlink" title="return"></a>return</h3><p>如果在一个函数中调用 return 语句，将立即结束此函数的执行并将它的参数作为函数的值返回。return 也会终止 eval() 语句或者脚本文件的执行。</p>
<p>如果在全局范围中调用，则当前脚本文件中止运行。如果当前脚本文件是被 include 的或者 require 的，则控制交回调用文件。此外，如果当前脚本是被 include 的，则 return 的值会被当作 include 调用的返回值。如果在主脚本文件中调用 return，则脚本中止运行。如果当前脚本文件是在 php.ini 中的配置选项 auto_prepend_file 或者 auto_append_file 所指定的，则此脚本文件中止运行。</p>
<blockquote>
<p>Note: 注意既然 return 是语言结构而不是函数，因此其参数没有必要用括号将其括起来。通常都不用括号，实际上也应该不用，这样可以降低 PHP 的负担。</p>
</blockquote>
<h3 id="require"><a href="#require" class="headerlink" title="require"></a>require</h3><p>require 和 include 几乎完全一样，除了处理失败的方式不同之外。require 在出错时产生 <strong>E_COMPILE_ERROR</strong> 级别的错误。换句话说将导致脚本中止而 include 只产生警告（<strong>E_WARNING</strong>），脚本会继续运行。</p>
<h3 id="include"><a href="#include" class="headerlink" title="include"></a>include</h3><p>include 语句包含并运行指定文件。</p>
<p>以下文档也适用于 require。</p>
<p>被包含文件先按参数给出的路径寻找，如果没有给出目录（只有文件名）时则按照 include_path 指定的目录寻找。如果在 include_path 下没找到该文件则 include 最后才在调用脚本文件所在的目录和当前工作目录下寻找。如果最后仍<strong>未找到文件</strong>则 include 结构会发出一条<strong>警告</strong>；这一点和 require 不同，后者会发出一个<strong>致命错误</strong>。</p>
<p>如果定义了路径——不管是绝对路径（在 Windows 下以盘符或者 \ 开头，在 Unix/Linux 下以 / 开头）还是当前目录的相对路径（以 . 或者 .. 开头）——include_path 都会被完全忽略。例如一个文件以 ../ 开头，则解析器会在当前目录的父目录下寻找该文件。</p>
<p>有关 PHP 怎样处理包含文件和包含路径的更多信息参见 include_path 部分的文档。</p>
<p>当一个文件被包含时，其中所包含的代码继承了 include 所在行的变量范围。从该处开始，调用文件在该行处可用的任何变量在被调用的文件中也都可用。不过所有在包含文件中定义的函数和类都具有全局作用域。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">vars.php</div><div class="line"><span class="meta">&lt;?php</span></div><div class="line"></div><div class="line">$color = <span class="string">'green'</span>;</div><div class="line">$fruit = <span class="string">'apple'</span>;</div><div class="line"></div><div class="line"><span class="meta">?&gt;</span></div><div class="line"></div><div class="line">test.php</div><div class="line"><span class="meta">&lt;?php</span></div><div class="line"></div><div class="line"><span class="keyword">echo</span> <span class="string">"A $color $fruit"</span>; <span class="comment">// A</span></div><div class="line"></div><div class="line"><span class="keyword">include</span> <span class="string">'vars.php'</span>;</div><div class="line"></div><div class="line"><span class="keyword">echo</span> <span class="string">"A $color $fruit"</span>; <span class="comment">// A green apple</span></div><div class="line"></div><div class="line"><span class="meta">?&gt;</span></div></pre></td></tr></table></figure>
<p>如果 include 出现于调用文件中的一个函数里，则被调用的文件中所包含的所有代码将表现得如同它们是在该函数内部定义的一样。所以它将遵循该函数的变量范围。此规则的一个例外是魔术常量，它们是在发生包含之前就已被解析器处理的。</p>
<h3 id="require-once"><a href="#require-once" class="headerlink" title="require_once"></a>require_once</h3><p>require_once 语句和 require 语句完全相同，唯一区别是 PHP 会检查该文件是否已经被包含过，如果是则不会再次包含。</p>
<h3 id="include-once"><a href="#include-once" class="headerlink" title="include_once"></a>include_once</h3><p>include_once 语句在脚本执行期间包含并运行指定文件。此行为和 include 语句类似，唯一区别是如果该文件中已经被包含过，则不会再次包含。如同此语句名字暗示的那样，只会包含一次。</p>
<p>include_once 可以用于在脚本执行期间同一个文件有可能被包含超过一次的情况下，想确保它只被包含一次以避免函数重定义，变量重新赋值等问题。</p>
<h3 id="goto"><a href="#goto" class="headerlink" title="goto"></a>goto</h3><p>goto 操作符可以用来跳转到程序中的另一位置。该目标位置可以用目标名称加上冒号来标记，而跳转指令是 goto 之后接上目标位置的标记。PHP 中的 goto 有一定限制，目标位置只能位于同一个文件和作用域，也就是说无法跳出一个函数或类方法，也无法跳入到另一个函数。也无法跳入到任何循环或者 switch 结构中。可以跳出循环或者 switch，通常的用法是用 goto 代替多层的 break。</p>
<p><strong>Example #1 goto 示例</strong></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="keyword">goto</span> a;</div><div class="line"><span class="keyword">echo</span> <span class="string">'Foo'</span>;</div><div class="line"> </div><div class="line">a:</div><div class="line"><span class="keyword">echo</span> <span class="string">'Bar'</span>;</div><div class="line"><span class="meta">?&gt;</span></div><div class="line"></div><div class="line">以上会输出：</div><div class="line">Bar</div></pre></td></tr></table></figure>
<p><strong>Example #2 goto 跳出循环示例</strong></p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"></div><div class="line"><span class="meta">&lt;?php</span></div><div class="line"><span class="keyword">for</span>($i=<span class="number">0</span>,$j=<span class="number">50</span>; $i&lt;<span class="number">100</span>; $i++) &#123;</div><div class="line">  <span class="keyword">while</span>($j--) &#123;</div><div class="line">    <span class="keyword">if</span>($j==<span class="number">17</span>) <span class="keyword">goto</span> end; </div><div class="line">  &#125;  </div><div class="line">&#125;</div><div class="line"><span class="keyword">echo</span> <span class="string">"i = $i"</span>;</div><div class="line">end:</div><div class="line"><span class="keyword">echo</span> <span class="string">'j hit 17'</span>;</div><div class="line"><span class="meta">?&gt;</span></div><div class="line"></div><div class="line">以上输出：</div><div class="line">j hit <span class="number">17</span></div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>


    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/PHP/" rel="tag"># PHP</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/05/16/PHP运算符/" rel="next" title="PHP运算符">
                <i class="fa fa-chevron-left"></i> PHP运算符
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/05/23/Laravel如何连接多个数据库？/" rel="prev" title="Laravel如何连接多个数据库？">
                Laravel如何连接多个数据库？ <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>

          
          </div>
          


          
  <div class="comments" id="comments">
    
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="http://gasbylei.github.io/uploads/avatar.png"
               alt="Gasbylei" />
          <p class="site-author-name" itemprop="name">Gasbylei</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
           
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives">
                <span class="site-state-item-count">17</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">6</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/Gasbylei" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#if"><span class="nav-number">1.</span> <span class="nav-text">if</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#elseif-else-if"><span class="nav-number">2.</span> <span class="nav-text">elseif/else if</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#流程控制的替代语法"><span class="nav-number">3.</span> <span class="nav-text">流程控制的替代语法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#while"><span class="nav-number">4.</span> <span class="nav-text">while</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#do-while"><span class="nav-number">5.</span> <span class="nav-text">do-while</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#for"><span class="nav-number">6.</span> <span class="nav-text">for</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#foreach"><span class="nav-number">7.</span> <span class="nav-text">foreach</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#用-list-给嵌套的数组解包"><span class="nav-number">7.1.</span> <span class="nav-text">用 list() 给嵌套的数组解包</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#break"><span class="nav-number">8.</span> <span class="nav-text">break</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#continue"><span class="nav-number">9.</span> <span class="nav-text">continue</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#switch"><span class="nav-number">10.</span> <span class="nav-text">switch</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#return"><span class="nav-number">11.</span> <span class="nav-text">return</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#require"><span class="nav-number">12.</span> <span class="nav-text">require</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#include"><span class="nav-number">13.</span> <span class="nav-text">include</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#require-once"><span class="nav-number">14.</span> <span class="nav-text">require_once</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#include-once"><span class="nav-number">15.</span> <span class="nav-text">include_once</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#goto"><span class="nav-number">16.</span> <span class="nav-text">goto</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Gasbylei</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    
    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>

  
  <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.0"></script>



  



  

    <script type="text/javascript">
      var disqus_shortname = 'gatsbylei';
      var disqus_identifier = '2017/05/17/PHP流程控制/';

      var disqus_title = "PHP流程控制";


      function run_disqus_script(disqus_script) {
        var dsq = document.createElement('script');
        dsq.type = 'text/javascript';
        dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/' + disqus_script;
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
      }

      run_disqus_script('count.js');

      
        var disqus_config = function () {
            this.page.url = disqus_url;
            this.page.identifier = disqus_identifier;
            this.page.title = disqus_title;
        };
        run_disqus_script('embed.js');
      

    </script>
  










  
  

  

  

  

  


  

</body>
</html>
